//
//  SEGWinRecordListViewController.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/21.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGWinRecordListViewController.h"
#import "SEGWinRecordListViewCell.h"
#import "SEGWinRecordDetailViewController.h"
#import "SEGActivityTriggerLotteryViewController.h"
#import "UIScrollView+SEGWindmill.h"
#import "SEGWinRecordListViewModel.h"


@interface SEGWinRecordListViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) SEGWinRecordListViewModel *viewModel;
/**
 主视图
 */
@property (nonatomic, strong)UITableView *tableView;


@end

@implementation SEGWinRecordListViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"中奖记录";
    
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    [self showCustomNavigationBackButton];
    
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf headerRereshing];
    } footerRefreshBlock:^{
        [weakSelf footerRereshing];
    }];
    [self.tableView beginHeaderRefreshing];
}

//左侧按钮
- (void)onNavigationLeftButtonClicked:(UIButton*)sender
{
    if (self.shouldPassTriggerLotteryView) {
        NSArray *viewControllers = self.navigationController.viewControllers;
        __block NSUInteger index = viewControllers.count - 2;
        [viewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj isKindOfClass:[SEGActivityTriggerLotteryViewController class]]) {
                index = idx - 1;
                *stop = YES;
            }
        }];
        [self.navigationController popToViewController:viewControllers[index] animated:YES];
    }else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//头部刷新逻辑
- (void)headerRereshing
{
    [self requsetActivitysWithRefresh:YES];
}

//底部刷新逻辑
- (void)footerRereshing
{
    [self requsetActivitysWithRefresh:NO];
}


- (void)requsetActivitysWithRefresh:(BOOL)refresh {
    //获取列表
    [[self.viewModel.listCommand execute:[NSNumber numberWithBool:refresh]] subscribeNext:^(id  _Nullable x) {
        [self.tableView stopAnimatingWindmill];
    } error:^(NSError * _Nullable error) {
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    } completed:^{
        if (self.viewModel.activityManager.winRecordsArray.count == 0) {
            NSDictionary *params =@{@"view":self.view};
            [[SEGUhomeMediator sharedInstance] seg_showCustomProgressImage:params];
        }
        else {
            [[SEGUhomeMediator sharedInstance] seg_hideCustomProgressImage:@{@"view":self.view}];
            [self.tableView reloadData];
        }
        [self.tableView reloadData];
    }];
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.activityManager.winRecordsArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SEGWinRecordListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kWinRecordListViewCellIdentifier forIndexPath:indexPath];
    [cell setUpCellWithModel:self.viewModel.activityManager.winRecordsArray[indexPath.section]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
    return 72.5;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SEGWinRecordDetailViewController *vc= [[SEGWinRecordDetailViewController alloc] initWithWinnerId:self.viewModel.activityManager.winRecordsArray[indexPath.section].winnerId];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - getter
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
        _tableView.backgroundColor = kColor_bg;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = kColor_L2;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        [_tableView registerClass:[SEGWinRecordListViewCell class] forCellReuseIdentifier:kWinRecordListViewCellIdentifier];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return _tableView;
}

- (SEGWinRecordListViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[SEGWinRecordListViewModel alloc] init];
    }
    return _viewModel;
}


@end
